from django import template
from likes.models import LikeCount,LikeRecord
from django.contrib.contenttypes.models import ContentType
from comment.forms import CommentForm



register = template.Library()

@register.simple_tag()
# 获取点赞数量
def get_like_count(obj):
    content_type = ContentType.objects.get_for_model(obj)
    like_count,created = LikeCount.objects.get_or_create(content_type=content_type,object_id=obj.pk)  ##点赞数
    return like_count.liked_num

@register.simple_tag(takes_context=True)
def get_like_status(context,obj):
    user = context['user']
    content_type = ContentType.objects.get_for_model(obj)
    if not user.is_authenticated:
        return ''
    if LikeRecord.objects.filter(content_type=content_type,object_id=obj.pk,user=user).exists():
        return 'active'
    else:
        return ''

@register.simple_tag()
def get_content_type(obj):
    content_type = ContentType.objects.get_for_model(obj)
    return content_type.model